# -*- coding: utf-8 -*-

"""
DateTime   : 2020/12/17 19:21
Author     : ZhangYafei
Description: 
"""
# 1. 定义生成器
def myList(num):
    now = 0           # 当前迭代值，初始为0
    while now < num:
        val = yield now                      # 返回当前迭代值，并接受可能的send发送值；
        now = now + 1 if val is None else val  # val为None，迭代值自增1，否则重新设定当前迭代值为val

# 2. 测试代码
my_list = myList(5)   # 得到一个生成器对象
print(my_list.__next__())  # 返回当前迭代值
print(my_list.__next__())
print(my_list.send(3))    # 重新设定当前的迭代值
print('next', next(my_list))
print(dir(my_list))    # 返回该对象所拥有的方法名，可以看到__iter__与next在其中

"""
0
1
3
4
['__class__', '__del__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__name__', '__ne__', '__new__', '__next__', '__qualname__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'close', 'gi_code', 'gi_frame', 'gi_running', 'gi_yieldfrom', 'send', 'throw']
"""